package com.feicheng.house.config;

import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SystemConfig implements WebMvcConfigurer {

	@Bean
	public ServletWebServerFactory webServerFactory() {
		TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
		fa.addConnectorCustomizers(
				(TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "[]{}"));
		return fa;
	}

	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
		return new RestTemplate(factory);
	}

	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		// 客户端与服务端建立连接超时时间
		factory.setConnectTimeout(1000);
		// 客户端从服务端读取数据的超时时间
		factory.setReadTimeout(2000);
		return factory;
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowCredentials(true).allowedOriginPatterns("*")
				.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTION").allowedHeaders("*").exposedHeaders("*");
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
	}

//	@Override
//	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(new HandlerInterceptor() {
//
//			private void authFailOutput(HttpServletResponse response) throws IOException {
//				response.setContentType("application/json;charset=UTF-8");
//
//				PrintWriter out = response.getWriter();
//				System.out.println("没有user");//
//				out.write(ResultInfo.loginFail().toJSONString());
//				out.flush();
//			}
//
//			@Override
//			public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
//					throws Exception {
//				System.out.println(request.getSession().getId());
//				if (request.getSession().getAttribute("CURRENT") == null) {
//					authFailOutput(response);// 失败就调用这个
//					return false;
//				}
//				System.out.println("有user");
//				return true;// 有user就不拦截
//			}
//		}).addPathPatterns("/subscribe/**", "/storePC/**").excludePathPatterns("/subscribe/system/login",
//				"/storePC/system/store-login");
//	}
}
